import Vue from "vue";
import menusArr from "@/router/menu";
import router from "@/router";
const state = {
  showMenu: true // 是否显示菜单信息
};
const mutations = {
  // 回到登录页
  TO_LOGIN() {
    this.commit("common/CLEAR_DATA");
    router.push({ name: "login" });
  },
  // 清除所有数据和状态(换肤不需要重置)
  CLEAR_DATA() {
    sessionStorage.clear();
    const defaultVuexState = Vue.prototype.$defaultVuexState;
    const defaultState = JSON.parse(JSON.stringify(defaultVuexState));
    const theme = this.state.theme;
    defaultState.theme = theme;
    this.replaceState(defaultState);
  },
  // 重新渲染菜单(传入新的菜单数组则替换成新菜单)
  RELOAD_MENU(state, newMenus) {
    if (newMenus) {
      menusArr.splice(0);
      newMenus.forEach(item => menusArr.push(item));
    }
    state.showMenu = false;
    Vue.prototype.$nextTick(() => {
      state.showMenu = true;
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations
};
